package com.webshop.Dao;

import com.webshop.database.ConnPool;
import com.webshop.database.JDBCUtil;
import com.webshop.domain.Items;
import com.webshop.domain.user.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class AdminDao {
    public static List<User> getUsers(){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<User> users = new ArrayList<User>();
        String sql = "select * from user;";
        try {
            conn = ConnPool.getConn();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                User user = new User();
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setModify(rs.getString("modify"));
                user.setItemsid(rs.getString("itemsid"));
                user.setUserid(rs.getInt("userid"));
                user.setName(rs.getString("name"));
                user.setTel(rs.getString("tel"));
                user.setEmail(rs.getString("email"));
                user.setSex(rs.getString("sex"));
                user.setState(rs.getString("state"));
                user.setBirthday(rs.getString("birthday"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.release(conn,ps,rs);
            return users;
        }
    }

    public static List<Items> getItems(){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Items> items = new ArrayList<Items>();
        String sql = "select * from electronic_product;";
        try {
            conn = ConnPool.getConn();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
               Items items1 = new Items();
               items1.setWhos(rs.getString("whos"));
               items1.setSinfo(rs.getString("sinfo"));
               items1.setAttrv(rs.getString("attrv"));
               items1.setAttr(rs.getString("attr"));
               items1.setPclass(rs.getString("pclass"));
               items1.setPimgsrc(rs.getString("pimgsrc"));
               items1.setPinfo(rs.getString("pinfo"));
               items1.setPname(rs.getString("pname"));
               items1.setPprice(rs.getInt("pprice"));
               items1.setId(rs.getInt("id"));
               items.add(items1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.release(conn,ps,rs);
            return items;
        }
    }

    public  static void deleteUser(User user){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<User> users = new ArrayList<User>();
        String sql = "delete from user where userid=?";
        try {
            conn = ConnPool.getConn();
            ps.setInt(1,user.getUserid());
            ps = conn.prepareStatement(sql);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.release(conn,ps,rs);
        }
    }


    public static List<User> searchName(String name){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<User> users = new ArrayList<User>();
        String sql = "select * from user where name=?;";
        try {
            conn = ConnPool.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1,name);
            rs = ps.executeQuery();
            while (rs.next()){
                User user = new User();
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setModify(rs.getString("modify"));
                user.setItemsid(rs.getString("itemsid"));
                user.setUserid(rs.getInt("userid"));
                user.setName(rs.getString("name"));
                user.setTel(rs.getString("tel"));
                user.setEmail(rs.getString("email"));
                user.setSex(rs.getString("sex"));
                user.setState(rs.getString("state"));
                user.setBirthday(rs.getString("birthday"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.release(conn,ps,rs);
            return users;
        }
    }


    public static List<User> searchUsername(String username){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        conn = ConnPool.getConn();
        List<User> users = new ArrayList<User>();
        String sql = "select * from user where username=?;";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            rs = ps.executeQuery();
            while (rs.next()){
                User user = new User();
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setModify(rs.getString("modify"));
                user.setItemsid(rs.getString("itemsid"));
                user.setUserid(rs.getInt("userid"));
                user.setName(rs.getString("name"));
                user.setTel(rs.getString("tel"));
                user.setEmail(rs.getString("email"));
                user.setSex(rs.getString("sex"));
                user.setState(rs.getString("state"));
                user.setBirthday(rs.getString("birthday"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.release(conn,ps,rs);
            return users;
        }
    }


    public static void update(String key,String value,int id){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        conn = ConnPool.getConn();
        String sql = "update user set  "+key+"=?  where userid=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,value);
            ps.setInt(2,id);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            JDBCUtil.release(conn,ps,rs);
        }
    }

    public static void main(String[] args) {
        List<User> users = searchName("ccccc");
        for(User u: users){
            System.out.println(u);
        }
    }
}
